package com.mhkj.workflow.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mhkj.common.util.HttpClientUtil;
import com.mhkj.workflow.entity.WorkFlowLogger;
import com.mhkj.workflow.service.WorkFlowLoggerService;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.util.UserHelper;
import com.mics.core.common.util.ftp.FtpUtil;

@Controller
@RequestMapping("/loggerService")
@Component
public class HttpClientUtils {

	@Autowired
	
	private WorkFlowLoggerService workFlowLoggerService;
	
	
    public static HttpClientUtils httpClientUtils;  // 关键2

    public HttpClientUtils() {
    }

    // 关键3
    @PostConstruct
    public void init() {
    	httpClientUtils = this;
    	httpClientUtils.workFlowLoggerService = this.workFlowLoggerService;
    	
    }

	@RequestMapping(value = "/startPost",method = RequestMethod.POST)
	@ResponseBody
	 public ResponseMessage startPost(String url,String jumpUrl,String formSign,String roleId,String workFlowId,Long id,String exampleEntity) throws IOException{
		
		Properties properties = new Properties();
		properties.load(FtpUtil.class.getResourceAsStream("/workflow.properties"));
		String thirdUrl=properties.get("url").toString();
		url=thirdUrl+url;
		Map<String,String> map = new HashMap<>();
		map.put("formSign", formSign);
		map.put("workFlowId", workFlowId);
		map.put("roleId", UserHelper.getCurrentEmployee().getRoleList().get(0).getId());
		map.put("user_id", UserHelper.getCurrentUserId());
		map.put("token", UserHelper.getCurrentEmployee().getToken());
		String message=HttpClientUtil.post(url, map);
		JSONObject jsStr = JSONObject.parseObject(message);

		String respCode=jsStr.get("respCode").toString();
		if(respCode.equals("1")) {
			String data=jsStr.get("data").toString();
			String procInsId=JSONObject.parseObject(data).get("procInsId").toString();
			
			WorkFlowLogger logger = new WorkFlowLogger();
			logger.setExampleId(id);
			logger.setProcInsId(procInsId);
			logger.setUrl(jumpUrl);
			logger.setToken(UserHelper.getCurrentEmployee().getToken());
			logger.setWorkFlowId(workFlowId);
			logger.setWorkFlowType(formSign);
			logger.setExampleEntity(exampleEntity);
			message=workFlowLoggerService.save(logger);
		}
		return ResponseMessage.createMessage(true, message);
	 }

	/**
	 * 获取流程的状态
	 * @param service
	 * @param projectBudgetService
	 * @param list
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/getActivityState")
	 public String getActivityState(WorkFlowLoggerService workService,List<WorkFlowLogger> list) throws IOException {	
		
		String message= workService.getActivityState(list);
		
		return message;
}
	 
/*	public static void main(String[] args) throws IOException {
		 Map<String,String> map = new HashMap<>();
		 map.put("workFlowId", "GC_XMGS");
		 map.put("user_id", "bf83618d06e243bd97cb530f079cb958");
		 map.put("roleId", "223ad5f86b394bd79efeb1347761bce9");
		 map.put("formSign", "XMGS_01");
		 map.put("token", "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiZjgzNjE4ZDA2ZTI0M2JkOTdjYjUzMGYwNzljYjk1OCIsImlhdCI6MTUyOTM5NzA0OSwic3ViIjoiIiwiaXNzIjoiIiwiZXhwIjoxNTI5NDMzMDQ5fQ.dIi5anZS-gZZN-M6rouMiD69hR9uF4_9Y50vL0LItko");
		 post("/act/taskApi/start",map);
		 
	 }*/
}
